<?php
class Manager_MenuController extends Zendvn_Controller_Action{
	private $_arrParam;
	
	private $_mainAction;
	
	protected $_currentController;
	
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	
	public  function init()
	{
	    $this->_arrParam = $this->_request->getParams();	
        $this->loadTemplate(TEMPLATE_PATH . '/admin/system');
        
        $this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam; 
	    
	     $tbl_menu             = new Manager_Model_menu();
	     $data = $tbl_menu->get_menu_parent();
	     $menu = array();
	     for($i=0;$i<count($data);$i++){
	         $menu[]=$data[$i];   	
	       }
        $newMenu = array();
        $result  = $tbl_menu-> get_content($menu,0,1,$newMenu);
      
       $this->view->parent=$result;
	  
        
         
    }
    
    public function indexAction(){
          $this->view->Title = "Administrator ::Menu Manager";
		  $this->view->headTitle($this->view->Title,true);
		
		$country=new Manager_Model_menu();
		$this->view->lang= $country->list_news();
		
		if($this->_request->getCookie('countries')==NULL){ $countries=-1; } 
			else { $countries=$this->_request->getCookie('countries');}	 
			
	    if($this->_request->getCookie('position')==NULL){ $position=-1; } 
			else { $position=$this->_request->getCookie('position');}	 
		
			
		if($this->_request->getPost('keyword')!=NULL){
			 $keyword=$this->_request->getParam('keyword');
			 $this->view->keyword=$keyword;
			
			 if($this->_request->getCookie('status')==NULL){ $status=-1; } 
			 else { $status=$this->_request->getCookie('status');}

     	 	 $listArticles = $country->searchkq($keyword,$status,$countries,$position);
     	 	  $menu = array();
					     for($i=0;$i<count($listArticles);$i++)
					       {
					         $menu[]=$listArticles[$i];   	
					       }
				        $newMenu = array();
				        $result  = $country-> get_content($menu,0,1,$newMenu);
     	 	 
     	 	 
          	 $currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
				 $currentPage = $i;
          	 }
         	 $paginator = Zend_Paginator::factory($result);
         	 $paginator->setItemCountPerPage(45)
                    ->setPageRange(5)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		 } 
    	else 
         {
			if($this->_request->getCookie('status')==NULL)
			  { 
				$status=-1;
			  } 
			else 
			  {
			  	    $status=$this->_request->getCookie('status');}
			 		$listArticles = $country->listItemabout($status,$countries,$position);
		     	 
				    $menu = array();
					     for($i=0;$i<count($listArticles);$i++)
					       {
					         $menu[]=$listArticles[$i];   	
					       }
				        $newMenu = array();
				        $result  = $country-> get_content($menu,0,1,$newMenu);
				  $currentPage = 1;
		         	 $i = $this->_getParam('page',1);
		         	 if(!empty($i))
		         	 {
		          	    $currentPage = $i;
		          	 }
         	 $paginator = Zend_Paginator::factory($result);
         	 $paginator->setItemCountPerPage(45)
                    ->setPageRange(10)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
	      }
     
    
     }
	
    public function addAction(){
    	
        $this->view->Title = "Menu Manager [New]";
		$this->view->headTitle($this->view->Title,true);
		
	    $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	    $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	    
	    

	    $tbl_menu             = new Manager_Model_menu();
	    $this->view->data     = $tbl_menu->get_section();
	    $this->view->result   =$tbl_menu->get_about();
	    $this->view->list_cat =$tbl_menu->get_list_cat();
	    $this->view->details_product=$tbl_menu->get_details_product();
	    $this->view->language=$tbl_menu->get_language();
	    $this->view->solution=$tbl_menu->get_solution();
	    
	   
	    
	    
       if($this->_request->isPost()==true)
		{	
		  $data=$this->_arrParam;
		   
		   $check=0;
           if ($data['title']==null)
	         {
	           $error="Enter you title menu";
	           $this->view->error=$error;
	            $check=1;
	          }
		   elseif ($data['type']==0)
	         {
	           $error="Select type menu.";
	           $this->view->error=$error;
	            $check=1;
	          }
		   elseif ($data['language_id']==0)
	         {
	           $error="Select language.";
	           $this->view->error=$error;
	            $check=1;
	          }
		 
	         
	          
		   if ($check==0)
	          {
	            $tbl_menu= new Manager_Model_menu();
	           
	            $tbl_menu->save($data);
	            $this->_redirect('manager/menu');
	          }
	      
		}
	// $this->_helper->viewRenderer->setNoRender();   
    }
    
    public function categoryAction(){
       
    	$this->_helper->viewRenderer->setNoRender();
	 	$this->_helper->layout->disableLayout();
    
    }
    
    public function editAction(){
    	
        $this->view->Title = "Administrator ::Add Menu Manager";
		$this->view->headTitle($this->view->Title,true);
		$id=$this->_request->getParam('id');
		
		$tbl_menu = new Manager_Model_menu();
	
		
		$this->view->data=$tbl_menu->get_section();
	    $this->view->result1=$tbl_menu->get_about();
	    $this->view->list_cat=$tbl_menu->get_list_cat();
	    $this->view->details_product=$tbl_menu->get_details_product();
	    $this->view->language=$tbl_menu->get_language();
	    $this->view->solution=$tbl_menu->get_solution();
		 
	    $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	    $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');

	    $tbl_menu          = new Manager_Model_menu();
	    $this->view->result= $tbl_menu->get_content_menu($id);
	    
    }
    
    public function statusAction(){
	    $db = Zend_Registry::get('connectDb');
        $arrParam=$this->_arrParam;
      if($this->_arrParam['type'] == 'multi'){
			if(count($arrParam['cid'])>0)
			{
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('menu',array('published'=>$status),$where);
			}
	     }
	   else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('menu',array('published'=>$status),$where);			
		   	 
	        }
	header("location:".$_SERVER['HTTP_REFERER']);
	 $this->_helper->viewRenderer->setNoRender();
	  }

    public function sortAction(){ 
		$db = Zend_Registry::get('connectDb');
		if($this->_request->isPost()){
		  
		foreach ($this->_arrParam['cid'] as $val){ 
		$where = 'id = ' . $val;
		$db->update('menu',array('order'=>$this->_arrParam['order'][$val]),$where);
		
		}
		     header("location:".$_SERVER['HTTP_REFERER']);
		     $this->_helper->viewRenderer->setNoRender();
		} 
}

    public  function deleteAction()
	{
		$res = new Manager_Model_menu();
		$id=$this->_request->getParam('id');
		$res->delete_data($id);
		header("location:".$_SERVER['HTTP_REFERER']);
	}

    public function mutiAction()
	{
	    $tblsection=new Manager_Model_menu();
		$tblsection->delete_muti($this->_arrParam);
		header("location:".$_SERVER['HTTP_REFERER']);
		$this->_helper->viewRenderer->setNoRender();
	}
}